import Vue from 'vue'
import VueRouter from 'vue-router'
import LoginView from '../views/LoginView.vue'
import RegisterView from '../views/RegisterView.vue'

// 解决导航栏或者底部导航tabBar中的vue-router在3.0版本以上频繁点击菜单报错的问题
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(error => error)
}

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Manager',
    component: () => import('../views/Manager.vue'),
    redirect: '/home',  // 重定向
    children: [
      { path: 'home', name: 'HomeView', component: () => import('../views/Manager/HomeView.vue'), meta: { requiresAuth: true, name: '主界面' } },
      { path: 'user', name: 'UserView', component: () => import('../views/Manager/UserView.vue'), meta: { requiresAuth: true, name: '用户信息' } },
      { path: 'euser', name: 'EuserView', component: () => import('../views/Manager/EuserView.vue'), meta: { requiresAuth: true, name: '员工管理' } },
      { path: 'euser2', name: 'EuserView', component: () => import('../views/Manager/EuserView2.vue'), meta: { requiresAuth: true, name: '大屏' } },
      { path: 'person', name: 'PersonView', component: () => import('../views/Manager/PersonView.vue'), meta: { requiresAuth: true, name: '个人信息' } },
      { path: 'password', name: 'Password', component: () => import('../views/Manager/Password.vue'), meta: { requiresAuth: true, name: '修改密码' } },
      { path: 'home7', name: 'Home7', component: () => import('../views/Manager/Home7.vue'), meta: { requiresAuth: true, name: 'home7' } },
      { path: 'home2', name: 'Home2', component: () => import('../views/Manager/Home2.vue'), meta: { requiresAuth: true, name: 'home2' } },
      { path: 'home3', name: 'Home3', component: () => import('../views/Manager/Home3.vue'), meta: { requiresAuth: true, name: 'home3' } },
      { path: 'home4', name: 'Home4', component: () => import('../views/Manager/Home4.vue'), meta: { requiresAuth: true, name: 'home4' } },
      { path: 'home5', name: 'Home5', component: () => import('../views/Manager/Home5.vue'), meta: { requiresAuth: true, name: 'home5' } },
      { path: 'home6', name: 'Home6', component: () => import('../views/Manager/Home6.vue'), meta: { requiresAuth: true, name: 'home6' } },
      { path: 'home8', name: 'Home8', component: () => import('../views/Manager/Home8.vue'), meta: { requiresAuth: true, name: 'home8' } },
    ],
  },
  {
    path: '/login',
    name: 'login',
    component: LoginView,
    meta: { requiresAuth: false, name: '登录' }, // 不需要 token

  },
  {
    path: '/register',
    name: 'register',
    component: RegisterView,
    meta: { requiresAuth: false, name: '注册' }, // 不需要 token

  },
  {
    path: '*',
    name: '404',
    component: () => import('../views/404.vue'),
    meta: { requiresAuth: true, name: '无法访问' },

  },

]



const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

//路由守卫
// to 是到达的路由信息    from 是来源的路由信息   next 是帮助跳转路由的函数 console.log(to,from,next)
router.beforeEach((to, from, next) => {
  //   const token = JSON.parse(localStorage.getItem('honey-user') || '{}');
  //   const requiresAuth = to.matched.some(record => record.meta.requiresAuth);

  //   if (requiresAuth && !token.token) {
  //     // 如果路由需要认证，且用户未认证，则重定向到登录页面
  //     next('/login');
  //   } else {
  //     // 如果用户已认证或路由不需要认证，继续访问目标路由
  next();
  //   }
});



export default router
